from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp.util import run_wsgi_app
import simplejson as json
import base64
import models


class all_rest(webapp.RequestHandler):
    """ gets all the restaurants on the db """
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        rests = db.GqlQuery("SELECT * from Restaurant")
        result = [{'name': str(rest.name), 'key': str(rest.key())} for rest in rests]
        self.response.out.write(json.dumps(result)) 

class all(webapp.RequestHandler):
    """ gets all the dishes and restaurants on the db"""
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        dishes = db.GqlQuery("SELECT * from Dish")
        d_queries = [] 
        for rest in dishes:
            d_queries.append(rest) 
        rests = db.GqlQuery("SELECT * from Restaurant")
        queries = [] 
        for rest in rests:
            queries.append(rest)
            #self.response.out.write( "%s \n"%rest.name)
        result = [{'restaurants':[{'name': str(q.name), 'key': str(q.key())} for q in queries],'dishes':[{'name': str(q.name), 'key': str(q.key())} for q in d_queries]}]
        self.response.out.write(json.dumps(result)) 
        
class rest_dishes(webapp.RequestHandler):
    """ gets all the dishes of a specific restaurant"""
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self_id = self.request.get('id')
        q=db.get(self_id);
        result = [{'name': str(d.name), 'rate': int(d.av_rate),'key':str(d.key())} for d in q.dishes]
        self.response.out.write(json.dumps(result)) 

class Detail(webapp.RequestHandler):
    """ get the details information for a specific dish """
    def get(self):
        self_id = self.request.get('id')
        self.response.headers['Content-Type'] = 'text/plain'
        q=db.get(self_id);    
        result = [{'name': str(q.name), 'id': int(q.key().id()), 'rate': int(q.av_rate),'img': base64.b64encode(q.img),'rate_num': int(q.no_rate),'dishtype_name':str(q.dishType.name),'dishtype_key':str(q.dishType.key()), 'loca_name':str(q.restaurant.name),'loca_key':str(q.restaurant.key()), 'ing':[str(db.get(d).name) for d in q.ingredients], 'com':[str(d) for d in q.comments] }]
        self.response.out.write(json.dumps(result))
        
class Dishes_type(webapp.RequestHandler):
    """ get all dishes of a specific type """
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self_id = self.request.get('id')
        #self.response.out.write( "%s \n"%self_id)
        q=db.get(self_id);
        result = [{'name': str(d.name), 'rate': int(d.av_rate),'key':str(d.key())} for d in q.dishes]
        self.response.out.write(json.dumps(result)) 

     
        
application = webapp.WSGIApplication([('/filter/dish_detail', Detail),('/filter/dish_type', Dishes_type),('/filter/all', all),('/filter/all_res', all_rest),('/filter/dishes', rest_dishes)],debug=True)
  
def main():
    run_wsgi_app(application)
    
if __name__ == "__main__":
    main()